package com.tysong.qq.utils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 对比工具类
 * @Project: qq
 * @Author: Sunday
 * @Create: 2021-09-01 20:55
 **/
public class Compared {

    public static List<String> getDifferent(List<String> list1, List<String> list2) {
        List<String> diff = new ArrayList<>();
        long start = System.currentTimeMillis();
        Map<String, Integer> map = new LinkedHashMap<>(list1.size() + list2.size());
        List<String> maxList = list1;
        List<String> minList = list2;
        if (list2.size() > list1.size()) {
            maxList = list2;
            minList = list1;
        }
        for (String string : maxList) {
            map.put(string, 1);
        }
        for (String string : minList) {
            Integer count = map.get(string);
            if (count != null) {
                map.put(string, ++count);
                continue;
            }
            map.put(string, 1);
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                diff.add(entry.getKey());
            }
        }
        System.out.println("方法4 耗时：" + (System.currentTimeMillis() - start) + " 毫秒");
        return diff;
    }
}
